package com.cloud.pole.rpc;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.weixin.service.WxPayService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/wxPay")
public class WxPayRest {

	@Autowired 
	private WxPayService wxPayService;
	
	/**
	 * 微信H5下单
	 * @param payJoinId
	 * @param outTradeNo
	 * @param totalFee
	 * @param openId
	 * @param redirectUri 修改订单状态接口 
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/unifiedOrderJsApi", method = RequestMethod.GET)
    public ApiRestResponse<String> unifiedOrderJsApi(
    		@RequestParam(name="payJoinId", defaultValue = "") Integer payJoinId,
    		@RequestParam(name="outTradeNo", defaultValue = "") String outTradeNo,
    		@RequestParam(name="totalFee", defaultValue = "") Long totalFee,
    		@RequestParam(name="openId", defaultValue = "") String openId,
    		@RequestParam(name="redirectUri", defaultValue = "") String callBackUri){
		try {
			ParamException.err(StringUtils.isBlank(outTradeNo), "outTradeNo不能为空");
			ParamException.err(StringUtils.isBlank(openId), "openId不能为空");
			ParamException.err(StringUtils.isBlank(callBackUri), "callBackUri不能为空");
			String strResponseMsg = wxPayService.unifiedOrderJsApi(payJoinId, outTradeNo, totalFee, openId, callBackUri);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, strResponseMsg);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
